﻿#include "popmenu.h"

PopMenu::PopMenu(QWidget* parent)
{
	main_vlay = new QVBoxLayout;
	main_vlay->setContentsMargins(5, 5, 5, 5);
	setLayout(main_vlay);
	setObjectName("popmenu");
	setStyleSheet("QWidget#popmenu{background-color:rgb(255,255,255);border:1px solid rgb(216, 216, 216)}");
	setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint | Qt::Tool);
}

void PopMenu::addAction(const QString& group, const QString& desc, const QString& icon)
{
	if (!menu_group.contains(group))
	{
		QVBoxLayout* temp_vlay = new QVBoxLayout;
		temp_vlay->setContentsMargins(0, 0, 0, 0);
		temp_vlay->setSpacing(0);
		main_vlay->addLayout(temp_vlay);
		menu_vlay[group] = temp_vlay;
	}
	QWidget* action_show = new QWidget;
	QHBoxLayout* action_hlay = new QHBoxLayout;
	ImgDisplay* action_icon = new ImgDisplay();
	action_icon->setWidth(30);
	action_icon->setHeight(30);
	action_icon->setVisual(icon.length() > 0);
	action_icon->setFixedSize(30, 30);
	QLabel* action_desc = new QLabel(desc);
	action_desc->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
	action_desc->setFixedHeight(30);
	action_hlay->setContentsMargins(5, 5, 5, 5);
	action_hlay->setSpacing(0);
	action_hlay->addWidget(action_icon);
	action_hlay->addWidget(action_desc);
	action_show->setLayout(action_hlay);
	action_show->setFixedWidth(250);
	menu_vlay[group]->addWidget(action_show);
	menu_group[group].push_back(action_show);
}

void PopMenu::setShowPoint(int x, int y)
{
	show_x = x;
	show_y = y;
}

void PopMenu::showEvent(QShowEvent* event)
{
	setGeometry(show_x, show_y, width(), height());
}

void PopMenu::focusOutEvent(QFocusEvent* event)
{
	hide();
}

void PopMenu::paintEvent(QPaintEvent* event)
{
	QPainter painter(this);
	QStyleOption opt;
	opt.initFrom(this);
	style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);
}
